package ups.apps.converter;

import java.util.List;

import javax.el.ELContext;
import javax.faces.application.Application;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;

import ups.apps.model.Usuario;

@FacesConverter("UsuarioConverter")
public class UsuarioConverter implements Converter {
	@Override
	public Object getAsObject(FacesContext context, UIComponent component,
			String value) {
		List<Usuario> users= this.getDAO();
		for(Usuario c : users){
			if (c.getUsrUsuario().equals(value))
				return c;
		}
		return null;		
	}

	@Override
	public String getAsString(FacesContext context, UIComponent component,
			Object value) {
			
		return ((Usuario) value).getUsrUsuario();
	}
	
	public List<Usuario> getDAO(){				
		FacesContext ctx = FacesContext.getCurrentInstance();
		ELContext ec = ctx.getELContext();
		Application app = ctx.getApplication();
		return (List) app.evaluateExpressionGet(ctx,"#{clienteBean.listUsuario}",List.class);		
	}
	
}
